﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using TTEntity;
using System.Data;
using System.Data.SqlClient;
namespace TTDataAccess
{
    /// <summary>
    /// Create by ThiepND 23/3/2011
    /// </summary>
    public class clsDAAdmin
    {
        clsDAConnection con = new clsDAConnection();
        public DataTable GetByUserPassword(string username, string password)
        {
            try
            {
                string str = "select * from tblAdmin where UserName=@tendangnhap and Password=@matkhau";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@tendangnhap", username);
                com.Parameters.AddWithValue("@matkhau", password);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable GetById(int id)
        {
            try
            {
                string str = "select * from tblAdmin where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable GetAll()
        {
            try
            {
                string str = "select * from tblAdmin";
                SqlCommand com = new SqlCommand(str, con.getCon());
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void Insert(clsAdmin obj)
        {
            try
            {
                string str = "insert into tblAdmin(UserName,Password,FullName,Email,OrderRequest)" +
                    " values(@UserName,@Password,@FullName,@Email,@OrderRequest)";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@UserName", obj.UserName);
                com.Parameters.AddWithValue("@Password", obj.Password);
                com.Parameters.AddWithValue("@FullName", obj.FullName);
                com.Parameters.AddWithValue("@Email", obj.Email);
                com.Parameters.AddWithValue("@OrderRequest", obj.OrderRequest);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Update(clsAdmin obj)
        {
            try
            {
                string str = "Update tblAdmin set UserName=@UserName,Password=@Password,FullName=@FullName," +
                    "Email=@Email,Address=@Address,OrderRequest=@OrderRequest where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@UserName", obj.UserName);
                com.Parameters.AddWithValue("@Password", obj.Password);
                com.Parameters.AddWithValue("@FullName", obj.FullName);
                com.Parameters.AddWithValue("@Email", obj.Email);
                com.Parameters.AddWithValue("@OrderRequest", obj.OrderRequest);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
        public void Delete(int id)
        {
            try
            {
                string str = "delete from tblAdmin where id=@id";
                SqlCommand com = new SqlCommand(str, con.getCon());
                com.Parameters.AddWithValue("@id", id);
                con.openCon();
                com.ExecuteNonQuery();
                con.closeCon();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                con.closeCon();
            }
        }
    }
}
